﻿using System;
using System.Web;

namespace Nexia.FluentHtmlElements.Encoders
{
	public class HtmlEncoder : IEncoder
	{
		/// <summary>
		/// Entitized special characters in the given input string.
		/// </summary>
		/// <param name="input">The input string</param>
		/// <returns></returns>
		public string Encode(string input)
		{
			return translateString(input, EncodeString);
		}

		/// <summary>
		/// De-entitize the special characters in the given intput string.
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public string Decode(string input)
		{
			return translateString(input, HttpUtility.HtmlDecode);
		}

		private static string translateString(string input, Func<string, string> translate)
		{
			return String.IsNullOrEmpty(input) ? String.Empty : translate(input);
		}

		internal virtual string EncodeString(string str)
		{
			return HttpUtility.HtmlEncode(str);
		}
	}
}
